<template>
  <MarkdownEditor
    style="z-index: 1"
    :toolbarsFlag="true"
    :content="markdownInfo.content"
    :navigation="true"
    @save="save"
  ></MarkdownEditor>
</template>
<script>
import MarkdownEditor from "_c/markdown";
import {
  getMarkdowns,
  createMarkdown,
  updateMarkdown,
} from "@/api/HttpAutoTestService/markdown";
export default {
  name: "help-docs",
  components: { MarkdownEditor },
  mounted() {
    getMarkdowns({ types: this.markdownInfo.types }).then((res) => {
      if (res.status === 200 && res.data.count) {
        const data = res.data;
        this.markdownInfo.id = data.results[0].id;
        this.markdownInfo.content = data.results[0].content;
      }
    });
  },
  data() {
    return {
      markdownInfo: {
        id: "",
        name: "接口测试帮助文档",
        types: "HTTP_TEST_HELP_DOC",
        content: "",
      },
    };
  },
  methods: {
    // 保存content
    save(value, render) {
      this.markdownInfo.content = value;
      if (this.markdownInfo.id) {
        updateMarkdown(this.markdownInfo).then((res) => {});
      } else {
        delete this.markdownInfo.id;
        createMarkdown(this.markdownInfo).then((res) => {
          if (res.status === 201) {
            this.markdownInfo.id = res.data.id;
          }
        });
      }
    },
  },
};
</script>
<style scoped>
</style>